package com.dsg.controller.account;


import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsg.aspect.NoRepeatSubmit;
import com.dsg.common.constants.MessageConstants;
import com.dsg.common.dto.AjaxResult;
import com.dsg.common.util.SingletonLoginUtils;
import com.dsg.common.util.toolbox.StringUtil;
import com.dsg.controller.base.BaseController;
import com.dsg.entity.account.DsgAccountCheckDetail;
import com.dsg.service.account.IDsgAccountCheckDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 开票記錄 前端控制器
 * </p>
 *
 * @author system
 * @since 2020-07-15
 */
@RestController
@RequestMapping("/account/dsgAccountCheckDetail")
@Api(description = "开票記錄")
public class DsgAccountCheckDetailController extends BaseController {

    @Autowired
    private IDsgAccountCheckDetailService dsgAccountCheckDetailService;

    /**
     * 查詢列表（分頁）
     *
     * @param dsgAccountCheckDetail
     * @return
     */
    @GetMapping(value = "/list")
    @ApiOperation("查詢列表（分頁）")
    public AjaxResult list(DsgAccountCheckDetail dsgAccountCheckDetail) {
        // 分页
        Page page = getPage();
        String sort = getParameter("sort");
        List<OrderItem> orderItemList = new ArrayList<>();
        OrderItem orderItem = new OrderItem();
        if (StringUtil.isEmpty(sort)) {
            orderItem.setColumn("A.STATUS,A.CHECK_NO");
            orderItem.setAsc(true);
            orderItemList.add(orderItem);
            page.setOrders(orderItemList);
        }
        dsgAccountCheckDetail.setEntCode(SingletonLoginUtils.getSystemEntCode());
        page = dsgAccountCheckDetailService.selectByList(page, dsgAccountCheckDetail);
        // 格式要返回的数据
        AjaxResult ajaxResult = json(MessageConstants.SSO_STATUS_SUCCESS, page.getRecords(), (int) page.getTotal());
        return ajaxResult;
    }

    /**
     * 更新
     *
     * @param dsgAccountCheckDetail
     * @return
     */
    @NoRepeatSubmit
    @PutMapping(value = "/update")
    @ApiOperation("更新")
    public AjaxResult update(@RequestBody DsgAccountCheckDetail dsgAccountCheckDetail) {
        boolean flag = dsgAccountCheckDetailService.updateById(dsgAccountCheckDetail);
        if (flag) {
            return json(MessageConstants.SSO_STATUS_SUCCESS, dsgAccountCheckDetail);
        } else {
            return result(MessageConstants.SSO_STATUS_FAIL);
        }
    }
}
